package com.ruoyi.project.modules.usage.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.project.modules.businessUsage.mapper.BusinessUsageMapper;
import com.ruoyi.project.modules.category.domain.Category;
import com.ruoyi.project.modules.category.mapper.CategoryMapper;
import com.ruoyi.project.modules.interfaceObject.domain.InterfaceObject;
import com.ruoyi.project.modules.interfaceObject.mapper.InterfaceObjectMapper;
import com.ruoyi.project.modules.interfaceinfo.domain.InterfaceInfo;
import com.ruoyi.project.modules.page.domain.Page;
import com.ruoyi.project.modules.role.domain.ParticipateRole;
import com.ruoyi.project.modules.role.mapper.ParticipateRoleMapper;
import com.ruoyi.project.modules.usage.domain.UsageParticipateRole;
import com.ruoyi.project.modules.usage.mapper.UsageParticipateRoleMapper;
import com.ruoyi.project.modules.userDataAuth.domain.UserDataAuth;
import com.ruoyi.project.modules.userDataAuth.mapper.UserDataAuthMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.modules.usage.mapper.UsageMapper;
import com.ruoyi.project.modules.usage.domain.Usage;
import com.ruoyi.project.modules.usage.service.IUsageService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 用况Service业务层处理
 *
 * @author smallrain
 * @date 2021-11-04
 */
@Service
public class UsageServiceImpl implements IUsageService {
    @Resource
    private UsageMapper usageMapper;
    @Resource
    private CategoryMapper categoryMapper;

    /**
     * 查询用况
     *
     * @param id 用况主键
     * @return 用况
     */
    @Override
    public Usage selectUsageById(Long id) {
        return usageMapper.selectUsageById(id);
    }

    /**
     * 查询用况列表
     *
     * @param usage 用况
     * @return 用况
     */
    @Override
    public List<Usage> selectUsageList(Usage usage) {
        return usageMapper.selectUsageList(usage);
    }

    @Override
    public List<Usage> selectUsageListV2(Usage usage) {
        List<Usage> list = usageMapper.selectUsageListV2(usage);
        if (null == list || list.size() == 0) {
            return null;
        }
        // 循环查询出用户角色
        list.forEach(a -> {
            UserDataAuth userDataAuth = new UserDataAuth();
            userDataAuth.setObjectId(a.getId());
            userDataAuth.setObjectType("2");
            List<UserDataAuth> userList = userDataAuthMapper.selectUserDataAuthListV2(userDataAuth);
            if (null == userList || userList.size() == 0) {
                return;
            }
            a.setUserList(userList);
        });
        return list;
    }

    /**
     * 新增用况
     *
     * @param usage 用况
     * @return 结果
     */
    @Override
    @Transactional
    public int insertUsage(Usage usage) {
        usage.setCreateTime(DateUtils.getNowDate());
        if (usage.getCategoryId() != null) {
            Category category = this.categoryMapper.selectCategoryById(usage.getCategoryId());
            usage.setCategoryCode(category.getCode());
        }
        int i = usageMapper.insertUsage(usage);
        insertRoles(usage.getId(), usage.getParticipateRoleIds());
        insertInterfaceObject(usage.getId(), usage.getInterfaceId());
        return i;
    }

    @Resource
    private UsageParticipateRoleMapper usageParticipateRoleMapper;

    private void insertRoles(Long id, Long[] participateRoleIds) {
        if (participateRoleIds != null && participateRoleIds.length > 0) {
            List<UsageParticipateRole> participateRoles = new ArrayList<>();
            for (Long roleId : participateRoleIds) {
                UsageParticipateRole participateRole = new UsageParticipateRole();
                participateRole.setParticipateRoleId(roleId);
                participateRole.setUsageId(id);
                participateRoles.add(participateRole);
            }

            this.usageParticipateRoleMapper.batchSave(participateRoles);
        }
    }

    /**
     * 修改用况
     *
     * @param usage 用况
     * @return 结果
     */
    @Override
    @Transactional
    public int updateUsage(Usage usage) {
        usage.setUpdateTime(DateUtils.getNowDate());
        usage.setUpdateBy(ShiroUtils.getLoginName());
        usage.setCreateBy(ShiroUtils.getLoginName());
        this.usageParticipateRoleMapper.removeByUsageId(Arrays.asList(usage.getId()));
        insertRoles(usage.getId(), usage.getParticipateRoleIds());
        this.interfaceObjectMapper.removeByObject(Arrays.asList(usage.getId()), "3");
        insertInterfaceObject(usage.getId(), usage.getInterfaceId());
        if (usage.getCategoryId() != null) {
            Category category = this.categoryMapper.selectCategoryById(usage.getCategoryId());
            usage.setCategoryCode(category.getCode());
        }
        return usageMapper.updateUsage(usage);
    }
    @Resource
    private InterfaceObjectMapper interfaceObjectMapper;

    private void insertInterfaceObject(Long id, Long interfaceId) {
        if (interfaceId != null) {
            InterfaceObject interfaceObject = new InterfaceObject();
            interfaceObject.setObjectId(id);
            interfaceObject.setInterfaceId(interfaceId);
            interfaceObject.setObjectType("3");
            this.interfaceObjectMapper.insertInterfaceObject(interfaceObject);
        }
    }

    @Resource
    private UserDataAuthMapper userDataAuthMapper;

    @Resource
    private BusinessUsageMapper businessUsageMapper;
    /**
     * 批量删除用况
     *
     * @param ids 需要删除的用况主键
     * @return 结果
     */
    @Override
    public int deleteUsageByIds(String ids) {
        this.interfaceObjectMapper.removeByObject(Convert.toLongList(ids), "3");
        userDataAuthMapper.removeByObjectId(Convert.toLongList(ids), "2 ");
        this.usageParticipateRoleMapper.removeByUsageId(Convert.toLongList(ids));
        this.businessUsageMapper.remove(Convert.toLongList(ids), null);
        return usageMapper.deleteUsageByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除用况信息
     *
     * @param id 用况主键
     * @return 结果
     */
    @Override
    public int deleteUsageById(Long id) {
        return usageMapper.deleteUsageById(id);
    }

    @Override
    public List<Usage> selectUsageListByBff(Set<Long> bffInterfaceIds) {
        return this.usageMapper.selectUsageListByBff(bffInterfaceIds);
    }
}
